package juc;

import java.util.concurrent.CompletableFuture;

/**
 * 两个任务结果相加
 */
public class CompleteableFutureTest3 {
    public static void main(String[] args) {
        //任务一
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
        System.out.println("执行任务1");
            return 1;
        }).thenApplyAsync((res)->{
        System.out.println("执行任务1+1");
            return res + 1;
        });
       /* CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("执行任务1");
            return 1;
        }).thenApply((res)->{
            System.out.println("执行任务1+1");
            return res + 1;
        });*/

        //任务二
        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("执行任务2");
            return 3;
        }).thenApplyAsync((res) -> {
            System.out.println("执行任务2+1");
            return res + 1;
        });
//        CompletableFuture<Integer> future2 = future.thenCombineAsync(future1, (task2, task1) -> {
//            return task1 + task2;
//        });

        //合并任务结果
        CompletableFuture<Integer> future2 = future.thenCombineAsync(future1, Integer::sum);

        System.out.println("最终结果：" + future2.join());
    }
}
